/*
 * RegistroOffset.cpp
 *
 *  Created on: 21/04/2013
 *      Author: pancho
 */

#include "RegistroOffset.h"
#include "RegistroTitulo.h"

RegistroOffset::RegistroOffset() {
	this->listaDeIds = new list<unsigned int>();
}

RegistroOffset::RegistroOffset(char *datos) {
	this->listaDeIds = new list<unsigned int>();
	unsigned int id;
	unsigned int posicion = 0;
	memcpy((char*)&this->Offset , &(datos[posicion]), sizeof(unsigned int));
	posicion += sizeof(unsigned int);
	memcpy((char*)&id, &(datos[posicion]), sizeof(unsigned int));
	this->agregarId(id);
}

list<unsigned int>* RegistroOffset::getId()
{
	return this->listaDeIds;
}


unsigned int RegistroOffset::getOffset()
{
	return this->Offset;
}

//Setters
void RegistroOffset::agregarId(unsigned int nuevoIdCancion)
{
	if(this->getId()->size()== 1)
	{
	if( nuevoIdCancion != this->getId()->front())
		{
			cout << "Error> Se intento agregar un Id distinto para un offset ya identificado(RegistroOffset)agregarId" << endl;
		}
	}
	this->listaDeIds->push_back(nuevoIdCancion);
}

void RegistroOffset::setOffset(unsigned int nuevoOffset)
{
	this->Offset = nuevoOffset;
}

int RegistroOffset::devolverDato(HashExtensible* unHash)
{
	return (unHash->fd(this->getId()->front()));
}

bool RegistroOffset::SosIgual(unsigned int IdCancion) {
	return (this->getId()->front() == IdCancion);
}

bool RegistroOffset::SosIgual(Registro* otroRegistro) {
	return otroRegistro->SosIgual(this);
}

//Se fija si son iguales en id
bool RegistroOffset::SosIgual(RegistroOffset* unRegistroOffset) {
	return (this->getId()->front() == unRegistroOffset->getId()->front());
}

bool RegistroOffset::SosIgual(RegistroTitulo* unRegistroTitulo) {
return false;
}

char* RegistroOffset::regToChar() {
	char* datos = new char[sizeof(unsigned int) * 2];
	unsigned int posicion = 0;
	memcpy(&(datos[posicion]), (char*)&this->Offset, sizeof(unsigned int));
	posicion += sizeof(unsigned int);
	memcpy(&(datos[posicion]), (char*)&(listaDeIds->front()), sizeof(unsigned int));
	return datos;
}

unsigned int RegistroOffset::getTamanioChar() {
	unsigned int tamanio = sizeof(unsigned int) * 2;
	return tamanio;
}

RegistroOffset::~RegistroOffset() {
	this->listaDeIds->clear();
	delete this->listaDeIds;
}


